/*
 * File Expression filename is undefined on line 4, column 24 in Templates/Licenses/license-default.txt.,Expression file is undefined on line 4, column 36 in Templates/Licenses/license-default.txt.
 * 
 */
package net.slavics.palm;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * @author slavic01
 * @version $Revision$
 * $LastChangedDate$ $Author$
 */
public class LoadDump {

    public static byte[] TBmp(BufferedReader in, int length) throws IOException {
        String str;
        byte[] bytes = new byte[length];
        int i = 0;
        while ((str = in.readLine()) != null) {
            if (str.isEmpty()) {
                break;
            }
            byte[] x = toCharArray(str.substring(8, 56));
            System.arraycopy(x, 0, bytes, i, x.length);
            i = +x.length;
        }
        return bytes;
    }

    public static byte[] toCharArray(String htext) {
        String x = "";
        htext = htext.replaceAll("  ", " ");
        String[] s = htext.split(" ");
        byte[] ret = new byte[s.length];
        for (int i = 0; i < s.length; i++) {
            int c = Integer.decode("0x" + s[i]);
            ret[i] = (byte) c;
        }
        return ret;
    }

    /**
     * Vrítí první objekt z dumpu
     * @return 
     */
    public static byte[] getFile() {
        try {
            BufferedReader in = new BufferedReader(new FileReader("kh.dmp"));
            String str;
            while ((str = in.readLine()) != null) {
                if (str.startsWith("Resource")) {
                    String[] head = str.split(",");
                    int length = Integer.valueOf((head[0].split(" "))[4]);
                    return TBmp(in, length);
                }
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println("Ok");
    }
}
